package com.credit.web.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchDataTransform;

import com.credit.context.CreditServletContext;
import com.credit.entity.CreditCoinRecord;
import com.credit.entity.CreditCouponEntity;
import com.credit.entity.CreditUserInfo;
import com.credit.exception.ErrorException;
import com.credit.service.CreditCouponService;
import com.credit.util.BeanToMapUtil;
import com.credit.util.CodeValueUtil;
import com.credit.util.ParamsConstant;
import com.credit.web.basic.bean.ResBean;

@Controller
@RequestMapping("/coupon")
public class CouponController {
	@Autowired
	private CreditCouponService couponService;
	
	@ResponseBody
	@RequestMapping(value = "/mylist", method = RequestMethod.POST)
	public void mylist(HttpServletRequest request, HttpServletResponse response,@RequestParam(value ="status",defaultValue="1000") Integer status,
			@RequestParam(value = "page",defaultValue = "1") int pageNumber) throws IllegalArgumentException, IllegalAccessException {
		
		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		Map<String, Object> searchParams = new TreeMap<String, Object>();
		searchParams.put("EQ_userId", userInfo.getId());
		if(Integer.valueOf(status).intValue()!=CodeValueUtil.ALL_CODE.intValue()){
			searchParams.put("EQ_status", status);
		}
		
		Map<String, SearchDataTransform> dataTyps = new HashMap<String, SearchDataTransform>();
		

		Page<CreditCouponEntity> page = couponService.getMyCouponListPage(searchParams, pageNumber, ParamsConstant.PAGESIZE, "createTime", dataTyps);
		List<CreditCouponEntity> couponList = page.getContent();
		List<Map<String,Object>> couponListReturn = new ArrayList<Map<String,Object>>();
		for(CreditCouponEntity coupon:couponList){
			couponListReturn.add(BeanToMapUtil.getMap(coupon));
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("couponList", couponListReturn);
		CreditServletContext.setResBean(resBean);
	}

}
